package com.service.impl;

import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.entity.User;
import com.service.UserService;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    UserDao userDao=new UserDaoImpl();
    public User login(String username,String passwd){
        User user = userDao.login(username, passwd);
        if (user!=null){
            return user;
        }
        return null;
    }

    @Override
    public int registered(String name, String pwd) {
        int registered = userDao.registered(name, pwd);
        return registered;
    }

    @Override
    public List<User> userList() {
        List<User> users = userDao.userList();
        return users;
    }

    @Override
    public int modify(User user) {
        int count = userDao.modify(user.getId(),user.getName(),user.getAddress(),user.getPhone(),user.getImage());
        return count;
    }
    @Override
    public User getUser(String name, String pwd){
        User  user = null;

        try {
            user = userDao.getUser(name,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }
    @Override
    public int modifyPwd(int id,String pwd){
        int count= 0;
        try {
            count = userDao.modifyPwd(id,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public User selectName(String name) {
        User count = userDao.selectName(name);
        if (count != null){
            return count;
        }else {
            return null;
        }
    }

    @Override
    public int getCartNum(int userId) {
        int cartNum=userDao.getCartNum(userId);
        if (cartNum!=0){
            return cartNum;
        }
        return 0;
    }

    @Override
    public User getUserById(int userId) {
        User user=userDao.getUserByID(userId);
        if (user!=null){
            return user;
        }
        return null;
    }

    public static void main(String[] args) {
        UserService userService=new UserServiceImpl();
        String user = userService.getUserById(10021).getUser();
        System.out.println(user);
    }
}
